/*
 * @(#)ShopInfoServiceImpl.java 2011-11-08
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shangjia.common.dao.support.Page;
import cn.shangjia.common.domain.Shop;
import cn.shangjia.common.domain.ShopDir;
import cn.shangjia.common.domain.ShopStatCode;
import cn.shangjia.common.domain.support.PostType;
import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.shop.config.Constants;
import cn.shangjia.shop.dao.ShopDao;
import cn.shangjia.shop.dao.ShopDirDao;
import cn.shangjia.shop.dao.ShopStatCodeDao;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.ShopInfoService;
import cn.shangjia.shop.service.impl.common.CommonService;

/**
 * 商铺服务实现类.
 *
 * @author <a href="mailto:wangxin@knet.cn">wangxin</a>
 * @version 1.0, 2011-11-08 17:33:05
 * @since 1.0
 */
@Service
public class ShopInfoServiceImpl implements ShopInfoService {
	
	private static final Logger logger = ShopLoggerFactory.getLogger(ShopInfoService.class);
	
	@Resource
	private ShopStatCodeDao shopStatCodeDao;
	
	@Resource
	private ShopDirDao shopDirDao;
	
	@Resource
	private ShopDao shopDao;
	
	@Resource
	private CommonService commonService;
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.ShopInfoService#findShopDirList(java.lang.String, cn.shangjia.common.domain.support.PostType, int, int)
	 */
	@Override
	public Collection<ShopDir> findShopDirList(String userId, PostType type, int num) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("type", type);
		
		Page<ShopDir> page = new Page<ShopDir>();
		page.setAutoCount(false);
		page.setPageNo(1);
		// 判断 是否 是 vip
		boolean isVip = commonService.isVip(userId);
		int maxNum = isVip ? Constants.siteSetting.getVipDirNum() : Constants.siteSetting.getUserDirNum();
		page.setPageSize(maxNum);
		
		Collection<ShopDir> dirList = shopDirDao.selectListPage(page, params).getResult();
		
		if (num < dirList.size()) {
			List<ShopDir> dirs = new ArrayList<ShopDir>(num);
			for (int i = 0; i < num; i ++) {
				dirs.add(dirList.iterator().next());
			}
			return dirs;
		}
		
		return dirList;
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.ShopInfoService#getShopByUserId(java.lang.String)
	 * jinzhongze 容错
	 */
	@Override
	public Shop getShopByUserId(String userId) {
		Map<String, Object> parames = new HashMap<String, Object>();
		parames.put("userId", userId);
		Shop shop = shopDao.selectOne(parames);
		if (shop == null)  { //容错机制,  防止脏数据 造成的页面崩溃
			logger.warn("商铺信息有脏数据[", userId, "]不存在Shop信息");
			shop = new Shop();
			shop.setUserId(userId);
		}
		return shop;
	}

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.ShopInfoService#getShopStatCodeListByUserId(java.lang.String)
	 */
	@Deprecated
	@Override
	public Collection<ShopStatCode> getShopStatCodeListByUserId(String userId) {
		Map<String, Object> parames = new HashMap<String, Object>();
		parames.put("userId", userId);
		return shopStatCodeDao.selectList(parames);
	}

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.ShopInfoService#getShopStatCodeByUserId(java.lang.String)
	 */
	@Override
	public ShopStatCode getShopStatCodeByUserId(String userId) {
		Map<String, Object> parames = new HashMap<String, Object>();
		parames.put("userId", userId);
		return shopStatCodeDao.selectOne(parames);
	}

}
